package com.common.weapp.service;

import java.io.IOException;

import com.common.user.domain.User;
import com.common.weapp.bean.WeJscode2SessionResult;
import com.common.weapp.bean.WeUser;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;

public interface WeUserService {

	String JSCODE_TO_SESSION_URL = "https://api.weixin.qq.com/sns/jscode2session?appid={appid}&secret={secret}&js_code={js_code}&grant_type={grant_type}";

	/**
	 * Get sessionKey use code.
	 */
	WeJscode2SessionResult getSessionInfo(String jsCode) throws Exception;

	/**
	 * Get user full info.
	 *
	 * @param sessionKey
	 *            sessionKey
	 * @param encryptedData
	 *            
	 * @param ivStr
	 * @throws IOException 
	 * @throws JsonMappingException 
	 * @throws JsonParseException 
	 *            
	 */
	WeUser getUserInfo(String sessionKey, String encryptedData, String ivStr) throws JsonParseException, JsonMappingException, IOException;

	/**
	 * Confirm the Info
	 *
	 * @param sessionKey
	 *          
	 * @param rawData
	 *           
	 * @param signature
	 *            
	 */
	boolean checkUserInfo(String sessionKey, String rawData, String signature);
	
	public static void weUser2User(User user, WeUser weUser) {
		user.setOpenId(weUser.getOpenId());
		user.setUsername(weUser.getOpenId());
		user.setNickName(weUser.getNickName());
		user.setGender(weUser.getGender());
		user.setLanguage(weUser.getLanguage());
		user.setCity(weUser.getCity());
		user.setProvince(weUser.getProvince());
		user.setCountry(weUser.getCountry());
		user.setAvatarUrl(weUser.getAvatarUrl());
		user.setUnionId(weUser.getUnionId());
	}
	

	public static void user2WeUser(User user, WeUser weUser) {
		weUser.setOpenId(user.getOpenId());
		weUser.setNickName(user.getNickName());
		weUser.setGender(user.getGender());
		weUser.setLanguage(user.getLanguage());
		weUser.setCity(user.getCity());
		weUser.setProvince(user.getProvince());
		weUser.setCountry(user.getCountry());
		weUser.setAvatarUrl(user.getAvatarUrl());
		weUser.setUnionId(user.getUnionId());
	}

}
